import type { App } from 'vue'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { createRouterGuards } from './router-guards'
import { PageEnum } from '@/enums/pageEnum'
import { HttpErrorPage, LoginRoute, ReloadRoute, RedirectRoute } from '@/router/base'
import { Layout } from '@/router/constant'

import modules from '@/router/modules'

const createRouterChild: any = () =>
  [
    ...HttpErrorPage,
    ...RedirectRoute,
    import.meta.env.VITE_MODE === 'lite' ? null : modules.projectRoutes,
    import.meta.env.VITE_MODE === 'lite' ? null : modules.chartRoutes,
    modules.previewRoutes,
    import.meta.env.VITE_MODE === 'lite' ? null : modules.editRoutes
  ].filter(Boolean)

const RootRoute: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Root',
    redirect: import.meta.env.VITE_MODE === 'lite' ? undefined : PageEnum.BASE_HOME,
    component: Layout,
    meta: {
      title: 'Root'
    },
    children: createRouterChild()
  }
]

export const constantRouter: any[] = [LoginRoute, ...RootRoute, ReloadRoute]

const router = createRouter({
  history: createWebHashHistory(''),
  routes: constantRouter,
  strict: true
})

export function setupRouter(app: App) {
  app.use(router)
  // 创建路由守卫
  createRouterGuards(router)
}
export default router
